//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

class ConditionServer001 : public CDeviceDriver
{
public:
    ConditionServer001()
    {
        pMutex = NULL;
        pCondition = NULL;
    }

    ~ConditionServer001()
    {
        if (pMutex) {
            delete pMutex;
        }

        if (pCondition) {
            delete pCondition;
        }
    }

    CARAPI Read(
        /* [in] */ UInt64 u64Offset,
        /* [in] */ UInt32 bytesToRead,
        /* [out] */ EzByteBuf buffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ UInt64 u64Offset,
        /* [in] */ EzByteBuf buffer,
        /* [out] */ UInt32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ EzByteBuf inBuffer,
        /* [out] */ EzByteBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose() {}

    CARAPI Init();

private:
    DzMutex       *pMutex;
    DzCondition   *pCondition;
};

EXTERN IDeviceDriver * CDECL CreateConditionServer0(uint_t uDeviceNo,
    void *pvParameter)
{
    ConditionServer001 *pDriver = new ConditionServer001;
    if (NULL == pDriver) {
        kprintf("ERROR:There have no enough memory, %s, %d\n",
                        __FILE__, __LINE__);
        return NULL;
    }

    ECode ec = pDriver->Init();
    if (FAILED(ec)) {
        return NULL;
    }

    pDriver->AddRef();

    return pDriver;
}

ECode ConditionServer001::Init()
{
    pMutex = new DzMutex;
    if (NULL == pMutex) {
        kprintf("ERROR:There have no enough memory, %s, %d\n",
                        __FILE__, __LINE__);

        return E_FAIL;
    }

    pCondition = new DzCondition;
    if (NULL == pCondition) {
        kprintf("ERROR:There have no enough memory, %s, %d\n",
                        __FILE__, __LINE__);

        return E_FAIL;
    }

    return NOERROR;
}

ECode ConditionServer001::Read(
    /* [in] */ UInt64 u64Offset,
    /* [in] */ UInt32 bytesToRead,
    /* [out] */ EzByteBuf buffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode ConditionServer001::Write(
    /* [in] */ UInt64 u64Offset,
    /* [in] */ EzByteBuf buffer,
    /* [out] */ UInt32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode ConditionServer001::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ EzByteBuf inBuffer,
    /* [out] */ EzByteBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (NULL != ppCompletionEvent || !pOutBuffer->IsNull()) {
        return E_INVALID_ARGUMENT;
    }

    ECode ec = NOERROR;

    switch (nControlCode) {
        case 0:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            pMutex->UninterruptibleLock();

            break;
        }

        case 1:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            pMutex->Unlock();

            break;
        }
        case 2:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            ec = pCondition->Wait(pMutex);

            break;
        }
        case 3:{
            if (inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            uint_t  uWaitTime;
            uint_t  uResidualTime;

            uWaitTime = *(uint_t *)(char *)inBuffer;

            SystemTime  currentTime;
            SystemTime  leastTime;

            DzGetSystemTime(&currentTime);
            leastTime.seconds = currentTime.seconds + uWaitTime / 1000;
            if (leastTime.seconds < currentTime.seconds) {
                leastTime.seconds = currentTime.seconds;
            }

            leastTime.microseconds = currentTime.microseconds +
                    (uWaitTime % 1000) * 1000;
            if (leastTime.microseconds < currentTime.microseconds) {
                leastTime.microseconds = currentTime.microseconds;
            }

            ec = pCondition->TryWait(pMutex, uWaitTime, &uResidualTime);

            if (S_TIMED_OUT == ec) {
                DzGetSystemTime(&currentTime);
                if (leastTime.seconds > currentTime.seconds) {
                    ec = E_UNEXPECTED;
                }
                else if (leastTime.seconds == currentTime.seconds) {
                    if (leastTime.microseconds > currentTime.microseconds) {
                        ec = E_UNEXPECTED;
                    }
                }
            }

            break;
        }

        case 4:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            pCondition->Pulse();

            break;
        }

        case 5:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            pCondition->PulseAll();

            break;
        }

        default:{
            return E_NOT_IMPLEMENTED;
        }
    }

    return ec;
}
